import React, { useEffect, useState } from 'react';
import axios from 'axios';


const request = async url => {
    const resp = await axios.get(url);
    const data = resp.data;
    if (data.code !== 200) {
        alert('服务错误');
        return {};
    }
    return data;
}

const useUser = (id) => {
    const [user, setUser] = useState({});
    // eslint-disable-next-line react-hooks/exhaustive-deps
    const getUser = async () => {
        const user = await request(`https://xiaozhu.run/api/user/${id}`);
        setUser(user.data);
    };
    useEffect(() => getUser(), [id])
    return { user }
}



const GetUser = () => {
    const [id, setId] = useState(9527);

    const { user } = useUser(id);


    return (

        <div>
            <button onClick={() => setId(id + 1)}>Get USer{id}</button>
            <div>Email:{user.email}</div>

            <div>UserName:{user.username}</div>
        </div>
    )

}

export default GetUser;